<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 2018/10/12
 * Time: 9:46
 */

namespace home\modules\docu\controllers;


use common\components\tools\Tools;
use common\models\db\Docu;
use common\models\db\DocuComment;
use common\models\db\DocuCommentReply;
use home\modules\docu\models\FormComment;
use home\modules\docu\models\FormCommentReply;
use home\modules\docu\models\FormDocuSearch;
use home\modules\docu\models\FormLookPwd;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;

class PublicController extends Controller
{
    public $index_title = "文档列表";
    public $index_docu_type = Docu::TYPE_ARTICLE;

    public function actionIndex()
    {
        $this->view->title = $this->index_title;
        $dataProvider = new ActiveDataProvider([
            'query'=>Docu::find()->where(['type' => $this->index_docu_type, 'status' => Docu::STATUS_DEFAULT])->orderBy(['id' => SORT_DESC]),
            'pagination'=>[
                'pageSize' => 10,
            ],
        ]);
        $form_docu_search = new FormDocuSearch();
        return $this->render('/public/_index', [
            'dataProvider' => $dataProvider,
            'form_docu_search' => $form_docu_search
        ]);
    }

    /**
     * @param $id
     * @return string
     * @throws
     */
    public function actionView($uuid)
    {
        $model = $this->_findDocuByUuid($uuid);
        $form_look_pwd = new FormLookPwd();
        if (!$model->canLook){
            if ($form_look_pwd->load(\Yii::$app->request->post())){
                if ($model->look_pwd == $form_look_pwd->pwd){
                    $model->setCanLookCache(true);
                    return $this->refresh();
                }else{
                    \Yii::$app->session->addFlash("error", "密码错误");
                }
            }
        }
        $form_comment = new FormComment();
        $form_reply = new FormCommentReply();
        $comment_query = DocuComment::find()->where(['docu_id' => $model->id, 'status' => DocuComment::STATUS_DEFAULT])->orderBy(['id' => SORT_DESC]);
        if (\Yii::$app->request->isPjax){
            if ($form_comment->load(\Yii::$app->request->post())&&$form_comment->validate()){
                $form_comment->entry();
            }
            if ($form_reply->load(\Yii::$app->request->post())&&$form_reply->validate()){
                $form_reply->entry();
            }
        }
        $commentDataProvider = new ActiveDataProvider([
            'query' => $comment_query,
            'pagination'=>[
                'pageSize' => 10,
                'pageSizeParam' => "comment_per_page",
                'pageParam' => 'comment_p',
            ],
        ]);
        $reply_query = DocuCommentReply::find()->where(['status' => DocuCommentReply::STATUS_DEFAULT]);
        $this->view->title = $model->id;
        return $this->render('/public/_view', [
            'model' => $model,
            'form_look_pwd' => $form_look_pwd,
            'form_comment' => $form_comment,
            'commentDataProvider' => $commentDataProvider,
            'form_reply' => $form_reply,
            'reply_query' => $reply_query,
        ]);
    }

    /**
     * @param $uuid
     * @return Docu|null
     * @throws
     */
    private function _findDocuByUuid($uuid)
    {
        $model = Docu::findOne(['uuid' => $uuid, 'status' => Docu::STATUS_DEFAULT]);
        if (!$model){
            throw new NotFoundHttpException("未找到或已删除");
            /*\Yii::$app->session->addFlash("error", "未找到或已删除");
            $this->goBack();*/
        }
        return $model;
    }
}